import React , {Component}from 'react';
// import {  } from 'antd';
import {Button,Modal} from 'fly';

import InputBox from '../../common/InputBox.js'
import Select from '../../common/Select.js'
import  Confirm from '../../common/Confirm.js'
import GetRolesMutation from "./mutation/GetRolesMutation"
import Relay from "react-relay";
import CreateUserMutation from "./mutation/CreateUserMutation"
import ReactKey from "../../util/ReactKey"
import FindByPhoneMutation from "./mutation/FindByPhoneMutation"


export default class Addstaff extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            roles:[],
            departname:{},
            partid :0,
            departmentforchoose:[],
            companyhint:'',
            mal:'',
            connection:'',
            ratepayer:'',
            disable:true,
            enabled:false,
            defaulstPass:0,
            theSameName:''
        }
        this.enterpriseId = sessionStorage.getItem('enterpriseId');
        this.permissionId = sessionStorage.getItem('permissionId');
        this.key;
        this.Style = {};
        this.roles = [{text:"请选择职务",value:"0"}];
        this.corresponding = {};
        this.correspondingen = {};
        this.pid = {};
        this.i = 0;
        this.j = 0;
        this.k = 0;
        this.existuid = '';
    }

    componentWillMount() {
        this.key = ReactKey.key
        Relay.Store.commitUpdate(
            new GetRolesMutation({
                enid:this.enterpriseId,
                isAll:'zhaobin'
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    let errorcode = response.getRoles.getRoles.errorCode
                    if(errorcode ==0){
                        let roles = response.getRoles.getRoles.roles;
                        roles.map((context,index)=>{
                            let name = context.name;
                            let rid = context.roleid.rid;
                            let enid = context.roleid.enid;
                            this.roles.push({"text":name,value:context.roleid.rid})
                            this.corresponding[rid]=name;
                            this.correspondingen[rid]=enid;
                            this.pid[rid] = context.pid
                        });

                        this.setState({
                            roles : this.roles
                        })
                    }
                }
            }
        )
        
        
        this.Style.addstaffview={
            textAlign:"center",
            display:"block",
            border:"3px solid green"
        }
        this.Style.addstaffview.border = "3px solid red";
        this.Style.addstaffview.width = 400;
        this.Style.addstaffview.height = 200;
        this.Style.addstaffview.fontSize = 30;
        this.Style.headerview={
            width:"634px",
            height:"44px",
            paddingLeft:"18px",
            lineHeight:"44px",
            display:"block",
            background:"#ccc",
            marginBottom:49

        }
    }

    state = { visible: false }
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    handleOk = (e) => {
        this.setState({
            visible: false,
        });
    }
    handleCancel = (e) => {
        this.setState({
            visible: false,
        });
    }
    handleClick(){
        //console.warn(111)
    }


    addworker(){

        let companyname1 = this.refs._enterpriseName.state.text;
        let taxpayerNo1 = this.refs._taxpayerNo.state.text;
        let telephone1 = this.refs._telephone.state.text;
        let enterpriseCode1 = this.refs._enterpriseCode.state.text;
        let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,32}$/;
        let re_telephone =/^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if(!this.valiadate("connection")){
            return;
        }
        if(!this.valiadate("companyhint")){
            return;
        }
        if(!this.valiadate("ratepayer")){
            return;
        }

        if(enterpriseCode1!=""){
            if(!this.valiadate("mal")){
                return;
            }
        }

            if(re_taxpayerNo.test(taxpayerNo1)){
                this.i = 1;
            }else {
                this.i = 0;
            }
        if(re_telephone.test(telephone1)){
            this.j = 1;
        }else {
            this.j = 0;
        }
        if(companyname1!=""){
            this.k = 1;
        }else {
            this.k = 0;
        }
        
        let depid
        if(this.refs.partname.state.value==''){
            depid =  this.refs.partname.state.defaultValue
        }else{
            depid = this.refs.partname.state.value;
        }
        let roleid = this.refs.roles.state.value
        if(this.i+this.j+this.k==3){
            Relay.Store.commitUpdate(
                new CreateUserMutation({
                    enterpriseId:this.enterpriseId,
                    name: this.refs._enterpriseName.state.text,
                    phone: this.refs._telephone.state.text,
                    password: this.refs._taxpayerNo.state.text,
                    email: this.refs._enterpriseCode.state.text,
                    depId: depid,
                    roleId: roleid,
                    roleName: this.corresponding[roleid],
                    pid: this.pid[roleid],
                    roEnId:this.correspondingen[roleid],
                    uid:this.existuid
                }),
                {
                    onFailure: () => {
                    },
                    onSuccess: (response) => {
                        let errorCode = response.createUser.createUser.errorCode;
                        if(errorCode==0){
                            let inde = this.state.departmentforchoose.indexOf(depid)
                            if(this.props.findAll==true){//如果点击全部员工
                                this.props.getAllPerson();
                            }else{
                                this.props.onchose(depid,inde)
                            }
                            this.setState({
                                visible:false
                            });
                        }else{

                        }
                    }
                }
            )
        }else {
            return;
        }

    }


    bluron(type){
       this.valiadate(type);
    }
    valiadate(type){

        let rs =false;
        switch (type){
            case 'companyhint':
                let companyname1 = this.refs._enterpriseName.state.text.trim();
                let myregexp = /^[A-Za-z\u4e00-\u9fa5]{2,20}$/;
                if(companyname1==""){
                    this.setState({
                        companyhint:"姓名不能为空"
                    });
                }else if(!myregexp.test(companyname1)){
                    this.setState({
                        companyhint:"姓名格式为2-20位中英字符",
                    });
                }else{
                    this.setState({
                        companyhint:"",
                    });
                    rs=true;
                }
                return rs;
            case 'mal':
                let enterpriseCode1 = this.refs._enterpriseCode.state.text;
                // let re_enterpriseCode = /^([0-9]|[a-z]|[A-Z]){3,15}@([0-9]|[a-z]|[A-Z]){1,5}[\.]([0-9]|[a-z]|[A-Z]){2,5}$/
                // let re_enterpriseCode =/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
                var re_enterpriseCode = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
                if(re_enterpriseCode.test(enterpriseCode1)){
                    this.enterpriseCode = enterpriseCode1;
                    this.setState({
                        mal:""
                    });
                    rs=true;
                }else if(enterpriseCode1==""){
                    this.setState({
                        mal:""
                    });
                }else{
                    this.setState({
                        mal:"邮箱格式错误，请重新输入"
                    });
                }
                return rs;

            case 'connection':
                let telephone1 = this.refs._telephone.state.text;
                let re_telephone =/^(((13[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
                let telephone;
                if(re_telephone.test(telephone1)){
                    telephone = telephone1;
                    Relay.Store.commitUpdate(
                        new FindByPhoneMutation({
                            phone:telephone1
                        }),
                        {
                            onFailure: () => {
                            },
                            onSuccess: (response) => {
                                let errorCode = response.findByPhone.findByPhone.errorCode;
                                if(errorCode=='000005'){
                                    let userModel = response.findByPhone.findByPhone.userModel;
                                    if(userModel!==null){
                                        this.setState({
                                            connection:"该用户已存在",
                                            // disable:false
                                        });
                                        this.existuid = userModel.uid
                                        this.refs._enterpriseName.setState({
                                            text:userModel.name,
                                        });
                                        this.theSameName=userModel.name;
                                        this.setState({
                                            enabled:true
                                        })

                                        this.refs._telephone.setState({
                                            text:userModel.phone
                                        });
                                        this.refs._taxpayerNo.setState({
                                            text:'666666',
                                        });
                                    }
                                }else if(errorCode=='000001'){
                                    this.setState({
                                        connection:" ",
                                        enabled:false
                                    });
                                    if(this.theSameName==this.refs._enterpriseName.state.text){
                                        this.refs._enterpriseName.setState({
                                            text:''
                                        });
                                        this.refs._taxpayerNo.setState({
                                            text:''
                                        });
                                    }


                                    this.refs._telephone.setState({
                                        text:telephone1
                                    });
                                }
                            }
                        }
                    )
                    this.setState({
                        connection:""
                    });
                    rs=true;
                }else if(telephone1==""){
                    this.setState({
                        connection:"手机号不能为空"
                    });
                }else {
                    this.setState({
                        connection:"手机号格式错误，请重新输入"
                    });
                }
                return rs;

            case 'ratepayer':
                // if(this.refs.defaultPass.checked==false){
                    let taxpayerNo1 = this.refs._taxpayerNo.state.text;
                    let re_taxpayerNo = /^([0-9]|[a-z]|[A-Z]){6,32}$/;
                    let taxpayerNo;
                    if(re_taxpayerNo.test(taxpayerNo1)){
                        taxpayerNo = taxpayerNo1;
                        this.setState({
                            ratepayer:""
                        });
                        rs=true;
                    }else if(taxpayerNo1==""){
                        this.setState({
                            ratepayer:"密码不能为空"
                        });
                    }else {
                        this.setState({
                            ratepayer:"密码格式为6-32位，请重新输入"
                        });
                    }
                    return rs;
                // }
        }
    }
    afterclose(){
        this.setState({
            companyhint:"",
            ratepayer:"",
            connection:"",
            mal:"",
            disable:false
        });
        this.key = ReactKey.key
    }


    render(){
        return (
            <Modal maskClosable={false}
                   className="people_addstaff---modal close_style_modal"
                   afterClose={this.afterclose.bind(this)}
                   visible={this.state.visible}
                   onOk={ this.addworker.bind(this)} onCancel={this.handleCancel}

                   style={{width:"634px",display:"block"}} title={"添加员工"} key={this.key}
            >
                <span style={{position:"absolute",display:"block",left:74,top:12}}>（用于账号登陆）</span>

                <div className="people_addstaff">
                    <InputBox defaultvalute="请输入手机号" className="_input__re_margin" maxlength={11} titletext="手机号" space="15px" style={{width:322,marginLeft:"15px",height:30,borderColor:"#efefef",color:"#999",paddingRight:65}} ref={'_telephone'}  bluron={this.bluron.bind(this,'connection')}/>
                    <span style={{color:"#ff6f64",position:"absolute",top:7,right:90}}>{this.state.connection}</span>
                    <i className="star1"> </i>
                </div>

                <div className="people_addstaff people_addstaff---nameItem">
                    <InputBox defaultvalute="请输入姓名" maxlength={10} className="_input__re_margin"  titletext="姓名" space="15px" style={{width:322,marginLeft:"15px",height:30,borderColor:"#efefef",color:"#999",marginTop:"20px",paddingRight:65}} disabled={this.state.enabled} ref={"_enterpriseName"} bluron={this.bluron.bind(this,'companyhint')} />
                    <span style={{color:"#ff6f64",position:"absolute",right:90,top:7}}>{this.state.companyhint}</span>
                    <i className="star1"> </i>
                </div>

                <div className="people_addstaff people_addstaff---pasItem">
                    <InputBox defaultvalute="请输入密码，6-32位的字母或数字组成" className="_input__re_margin" maxlength={32}  isloginintext={true} titletext="密码" space="15px" style={{width:322,marginLeft:"15px",height:30,borderColor:"#efefef",color:"#999",marginTop:20,paddingRight:65}} disabled={this.state.enabled} ref={"_taxpayerNo"} type={'password'} bluron={this.bluron.bind(this,'ratepayer')}/>
                    <span style={{color:"#ff6f64",position:"absolute",right:90,top:7}}>{this.state.ratepayer}</span>
                    <i className="star1"> </i>
                </div>


                {/*<label style={{display:"block",marginLeft:94,marginTop:20}}><input type="checkbox" style={{display:"inlineBlock",marginRight:5}} ref={'defaultPass'} value={this.state.defaulstPass}/>设为默认密码</label>*/}
               <div className="people_addstaff">
                   <InputBox defaultvalute="请输入邮箱" titletext="邮箱" maxlength={30} className="_input__re_margin" space="15px" style={{width:322,marginLeft:"15px",height:30,borderColor:"#efefef",color:"#999",marginTop:20,paddingRight:65}} ref={"_enterpriseCode"} bluron={this.bluron.bind(this,'mal')}/>
                   <span style={{color:"#ff6f64",position:"absolute",right:90,top:7}}>{this.state.mal}</span>
               </div>


                <div className="people_addstaff people_muchmodifypart--select nth_sel_3">
                   <span style={{position:"absolute",display:"block",left:42,top:25}}>部门</span>
                   <Select oArr={this.state.departname} style={{width:335,height:30,marginTop:20,marginLeft:82}} ref={'partname'} defaultValue={this.state.partid}/>
                </div>
                <div className="people_addstaff people_muchmodifypart--select nth_sel_3">
                    <span style={{position:"absolute",display:"block",left:42,top:25}}>职务</span>
                    <Select oArr={this.state.roles} style={{width:335,height:30,marginTop:20,marginLeft:82}} ref={'roles'}/>
                </div>
            </Modal>
            )
    }
}


